package com.huawei.hwid.api.common;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import com.huawei.cloudservice.CloudAccount;
import com.huawei.cloudservice.CloudAccountManager;
import com.huawei.cloudservice.LoginHandler;
import com.huawei.hwid.c.a;
import com.huawei.hwid.core.constants.HwAccountConstants;
import com.huawei.hwid.core.d.b;
import com.huawei.hwid.core.d.b.e;
import com.huawei.hwid.core.datatype.HwAccount;
import com.huawei.hwid.core.helper.handler.ErrorStatus;
import com.huawei.secure.android.common.intent.SafeBroadcastReceiver;

class n extends SafeBroadcastReceiver {
    private Context a;
    private LoginHandler b;
    private boolean c = false;
    private String d;

    public n(Context context, LoginHandler loginHandler, String str) {
        this.a = context;
        this.b = loginHandler;
        this.d = str;
    }

    public void onReceiveMsg(Context context, Intent intent) {
        if (!this.c) {
            try {
                Bundle bundle = new Bundle();
                bundle.putBoolean("OpenLoginBroadcastReceiver", true);
                e.a(this.a, bundle);
            } catch (IllegalArgumentException e) {
                e.c("OpenLoginBroadcastReceiver", "IllegalArgumentException", true);
            }
            this.c = true;
            if (intent != null) {
                a(context, intent);
            }
        }
    }

    private void a(Context context, Intent intent) {
        e.b("OpenLoginBroadcastReceiver", "handleOpenLoginBroadcast", true);
        String action = intent.getAction();
        Bundle extras = intent.getExtras();
        if (action != null) {
            try {
                if (action.equals("com.huawei.hwid.opensdk.smsauth.success") || action.equals("com.huawei.hwid.opensdk.smsauth.quicklogin.SUCCESS")) {
                    if (extras != null) {
                        if ("com.huawei.hwid".equals(this.d)) {
                            extras.putString("serviceToken", b.a(extras.getString("serviceToken"), this.a.getPackageName()));
                        }
                        extras.putString("accountType", "2");
                        HwAccount a = o.a(this.a, extras);
                        Object q = a.q();
                        if (TextUtils.isEmpty(q) || HwAccountConstants.NULL.equalsIgnoreCase(q)) {
                            action = com.huawei.hwid.core.d.n.a(context, 0);
                            if (action == null) {
                                action = "";
                            }
                            a.p(action);
                        }
                        a.a(context).a(context, a);
                        CloudAccount[] a2 = e.a(context);
                        action = "";
                        if (!TextUtils.isEmpty(a.b())) {
                            action = a.b();
                        }
                        this.b.onLogin(a2, e.a(a2, action));
                    }
                } else if (action.equals("com.huawei.hwid.opensdk.switch.other")) {
                    Bundle bundle = new Bundle();
                    bundle.putInt(CloudAccountManager.KEY_LOGIN_CHANNEL, Integer.parseInt(com.huawei.hwid.core.d.a.a(context, b.c(context))));
                    e.a(context, b.c(context), bundle, this.b);
                } else if (action.equals("com.huawei.hwid.opensdk.smsauth.quicklogin.fail")) {
                    this.b.onError(new ErrorStatus(3002, "press back key"));
                }
            } catch (RuntimeException e) {
                e.b("OpenLoginBroadcastReceiver", "BroadcastReceiver components are not allowed to register to receive intents", true);
            }
        }
    }
}
